package com.ymm.driver.core.facade;

import com.ymm.driver.api.constants.Dto.CargoDto;
import com.ymm.driver.api.facade.DriverFindAllOrdersService;
import com.ymm.driver.dao.mapper.DriverFindAllOrdersMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Company: CUG
 * @Description: TODO
 * @Author: LiYangyong
 * @Date: 2022/8/5/005 7:14
 **/

@Component
@Service
public class DriverFindAllOrdersServiceImpl implements DriverFindAllOrdersService {


    @Resource
    DriverFindAllOrdersMapper driverFindAllOrdersMapper;


    /**
     * 司机调用系统端接口获取货源列表
     * @param driverLat
     * @param driverLon
     * @param driverCarType
     * @return
     */
    @Override
    public List<CargoDto> dayCargoService(Double driverLat, Double driverLon, Integer driverCarType) {

        List<CargoDto> cargoDto = driverFindAllOrdersMapper.selectCargo();

        // 创建列表用于存放符合要求的货源
        List<CargoDto> giveDriverCargoList = new ArrayList<>();


        int cargoLen = cargoDto.size();
        if (cargoLen < 1) {
            // 如果取到的货源为空
            return new ArrayList<>();
        }


        for (int i = 0; i < cargoLen; i++) {
            // 获取货源经纬度
            Double cargoSrcLat = cargoDto.get(i).getCargoSrcLat();
            Double cargoSrcLon = cargoDto.get(i).getCargoSrcLon();

            // 获取货源需要的车型
            Integer cargoRequireType = cargoDto.get(i).getCargoRequireType();

            double algorithm = algorithm(driverLat, driverLon, cargoSrcLat, cargoSrcLon);

            // 测试
            System.out.println(algorithm);

            // 判断距离是否小于10公里，并且车型相同
            if (algorithm(driverLat, driverLon, cargoSrcLat, cargoSrcLon) <= 10000 && cargoRequireType == driverCarType) {
                giveDriverCargoList.add(cargoDto.get(i));
            }
        }
        int temp = giveDriverCargoList.size();

        if (temp <= 0) {
            return new ArrayList<>();
        }

        return giveDriverCargoList;






    }



    // 距离单位  米
    // 距离单位  米
    // 根据两点经纬度计算距离
    public static double algorithm(double longitude1, double latitude1, double longitude2, double latitude2) {
        double Lat1 = rad(latitude1); // 纬度

        double Lat2 = rad(latitude2);

        double a = Lat1 - Lat2;//两点纬度之差

        double b = rad(longitude1) - rad(longitude2); //经度之差

        double s = 2 * Math.asin(Math

                .sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));//计算两点距离的公式

        s = s * 6378137.0;//弧长乘地球半径（半径为米）

        s = Math.round(s * 10000) / 10000;//精确距离的数值

        return s;

    }

    private static double rad(double d) {
        return d * Math.PI / 180.00; //角度转换成弧度

    }








}


